package work.util.editor;

import java.io.File;
import java.io.IOException;

import work.droit.gui.ApiController;
import work.droit.gui.EkiEditor;
import work.droit.parametre.Parametres;
import work.util.data.io.ManipFile;

public class EditorLauncher
{
	public static String FILE_EXTENSION = ".html";
	
	public static void launchEditor(final String fileName, final String docName, final String docType)
	{
		String path = Parametres.getInstance().getPathEditor();
		if (path == null || "".equals(path))
		{
			EkiEditor.launchEditor(true, fileName, docName, docType);
		}
		else
		{
			String sDocument = ApiController.pathDocument + fileName + FILE_EXTENSION;
			File file = new File(sDocument);
			try
			{
				if (!file.exists())
				{
					file.createNewFile();
					// on ajoute les balises html
					ManipFile.saveContent(sDocument, "<html></html>");
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}

			String command = path + " " + file.getAbsolutePath();

			try
			{
				System.out.println(command);
				@SuppressWarnings("unused")
				Process child = Runtime.getRuntime().exec(command);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void launchViewer(final String fileName, final String docName, final String docType)
	{
		String path = Parametres.getInstance().getPathViewer();
		if (path == null || "".equals(path))
		{
			EkiEditor.launchEditor(false, fileName, docName, docType);
		}
		else
		{
			String sDocument = ApiController.pathDocument + fileName + FILE_EXTENSION;
			File file = new File(sDocument);
			try
			{
				if (!file.exists())
					file.createNewFile();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}

			String command = path + " " + file.getAbsolutePath();

			try
			{
				System.out.println(command);
				@SuppressWarnings("unused")
				Process child = Runtime.getRuntime().exec(command);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
}
